//
//  HMEmoticonBottomView.swift
//  表情键盘
//
//  Created by iOS on 16/9/26.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit

enum HMEmoticonBottomViewType: Int {
    case Recent = 0
    case Default
    case Emoji
    case Lxh
}

// 1.定义协议
protocol HMEmoticonBottomViewDelegate: NSObjectProtocol {
    // 2.定义方法
    func didSelected(type: HMEmoticonBottomViewType)
}

/// 底部4个按钮的父控件
class HMEmoticonBottomView: UIView {
    
    // 3.代理属性
    weak var delegate: HMEmoticonBottomViewDelegate?
    
    /// 当前不可用(高亮)的按钮
    private var disableButton: UIButton?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        selectSection(section: 0)
    }

    @IBAction func buttonDidClick(button: UIButton) {
//        print("button: \(button.tag)")
        
        selectButton(button: button)
        
        // 让键盘去切换collectionView
        let type = HMEmoticonBottomViewType(rawValue: button.tag)!
        // 4.调用代理
        delegate?.didSelected(type: type)
    }
    
    /// 选中对应这组的按钮
    func selectSection(section: Int) {
        // 根据section找到对应的按钮
        let button = self.subviews[section] as! UIButton
        
        selectButton(button: button)
    }
    
    func selectButton(button: UIButton) -> Void {
        
        // 1.让之前不可用的变成可用的
        disableButton?.isEnabled = true
        
        // 2.让点击的变成不可用的(高亮)
        button.isEnabled = false
        
        // 3.将点击的按钮赋值到当前不可用的按钮
        disableButton = button
    }
}
